<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * M_login
 *
 * This is an M_login of a few basic login interaction methods you could use
 * all done with a hardcoded array.
 *
 * @package		CodeIgniter
 * @subpackage	Rest Server
 * @category	Controller
 * @author		Phil Sturgeon
 
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class Pay extends REST_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->model('mdl_pay');
		
	}
/*	
	function login_get()
    {
        
		
		$rows = array();
		$login = array();
		
		
		$page=$this->get('page');
		$limit=$this->get('lim');
		$key=$this->get('key');
		$value=$this->get('val');
		$countitem = 0;
		if (($key!=null) && ($page!=null) ){
			
			$arrwhere=$key." LIKE '%".$value."%'";
			$query = $this->mdl_login->get_item($limit, $page, $arrwhere);
			$countitem = $this->mdl_login->get_countitem($arrwhere);
		}elseif($limit!=null){
		
			$query = $this->mdl_login->get_item($limit, $page);
			$countitem = $this->mdl_login->get_countitem();
		}else{
			$query = $this->mdl_login->get_item();
			$countitem = $this->mdl_login->get_countitem();
		}
		
		if ($query->num_rows() > 0)
		{
			
			$i=0;
			foreach($query->result_array() as $row)
			{
				$rows[$i]['userid'] = $row['userid'];
				$rows[$i]['username'] = $row['username'];
				$rows[$i]['password'] = $row['password'];
				$rows[$i]['level_id'] = $row['level_id'];

				$i++;
			}
			$data = array(
				'countitem' => $countitem,
				'datas' => $rows
				
			);
		}
		
		//print_r($data);
		
        if($data)
        {
            $this->response($data, 200); // 200 being the HTTP response code
        }

        else
        {
            $this->response(array('error' => 'Items could not be found'), 404);
        } 
    }
    */
   /* function login_post()
    {
        //$this->some_model->updatelogin( $this->get('id') );
     
		
$data['username']=$this->post('username'); 
$data['password']=$this->post('password'); 
$data['level_id']=$this->post('level_id'); 



		$query = $this->mdl_login->insert($data);
		
	
		if ($query){
			
			$this->response(array('status' => 'true'),200);
		}else{
			$this->response(array('status' => 'false'));
		}

    }
	*/
	
	function check_pay_post(){
		       //$this->some_model->updatelogin( $this->get('id') );
        
		//post
		$data =array();
		$username=$this->post('username'); 
		$appsid=$this->post('appsidonapps'); 
		$channelid=$this->post('channelid'); 
		$mappsid=$this->post('mappsid');
		$serviceid=$this->post('serviceid'); 
		$amount=$this->post('amount'); 
		$referenceid=$this->post('referenceid'); 
		$paymethodid=$this->post('paymethodid'); 
		$operator=$this->post('operator'); 
		$this->load->helper('jwt_helper');
		$token = JWT::decode($_POST['token'], 'dompeto');
		
		$appsidonappstoken= $token->appsidonapps;
        $usernametoken= $token->username;
        	
		
		
		
		
	
		
		
		
		if($appsid==$appsidonappstoken && $username==$usernametoken) {
		
			$arrwhere="referenceid = '" . $referenceid . "'";
			$queryx = $this->mdl_pay->get_item(0, 0, $arrwhere);
			$resultx = $queryx->result_array();
			$trxstatus = $resultx[0]['trxstatus'];
			$operator = $resultx[0]['musername'];
			$channelid = $resultx[0]['channelid'];
			$serviceid = $resultx[0]['serviceid'];
			$mappsid = $resultx[0]['mappsid'];
		$amount = $resultx[0]['amount'];
			
			
			if($queryx) {
		
		if($trxstatus==0)
		{
		
		$data['username']=$username;
		$data['appsid']=$appsid;
		$data['mappsid']=$mappsid;
		
		$this->load->helper('date');
		$datestring = "%Y-%m-%d %h:%i:%s";
		$time = time();
		$closetrxdate=mdate($datestring, $time);

		$data['closetrxdate']=$closetrxdate;
		$data['paystatus']='true';
		$data['trxstatus']='0';    //  di bypass dulu
		$data['paymethodid']=$paymethodid;
		$data['appsid']=$appsid;
		
		$query = $this->mdl_pay->updatereferenceid($referenceid, $data);
		
		// insert inbox for merchant
		$message= array(
				'userinbox' =>$operator,
				'usercat' =>'channel',
				'channelid' =>$channelid,
				'serviceid'=>$serviceid,
				'referenceid' => $referenceid,
				'mappsid' =>$mappsid,
				'operator'=>$operator,
				'amount'=>$amount,
				'closetrxdate'=>$closetrxdate,
				'username'=>$username,
				'appsid'=>$appsid,
				'debug' => 'new message'
				);
		
		
		
		$query2 = $this->mdl_pay->insertinbox($message);
		
			// insert inbox for user
		$message= array(
				'userinbox' =>$username,
				'usercat' =>'user',
				'channelid' =>$channelid,
				'serviceid'=>$serviceid,
				'referenceid' => $referenceid,
				'mappsid' =>$mappsid,
				'operator'=>$operator,
				'amount'=>$amount,
				'closetrxdate'=>$closetrxdate,
				'username'=>$username,
				'appsid'=>$appsid,
				'debug' => 'new message'
				);
		
		
		
		$query2 = $this->mdl_pay->insertinbox($message);
		
				$pay[] = array(
				
				'Channelid' =>$channelid,
				'serviceid'=>$serviceid,
				'paystatus' => 'true',
				'paymethod' => $paymethodid,
				'paynotification' => 'SUKSES',
				'debug' => 'update transaction'
				);
		} else {
				$pay[] = array(
				
				'paystatus' => 'false',
				'paynotification' => 'gagal',
				'debug' => 'pay referenceid uda close'
				);

		}


		} else 
			{
						
				$pay[] = array(
				
				'paystatus' => 'false',
				'paynotification' => 'gagal',
				'debug' => 'pay referenceid salah'
				);
			}
			
		
		
		
		
		
	
	}
	else {
	
	
	}
	
	 $this->response($pay, 200);	
	}
	

    /*
    function login_delete()
    {
		
    	//$this->some_model->deletesomething( $this->get('id') );$this->_get('id')
        $message = array('message' => 'DELETED!', 'id'=>$this->delete('id'));
		
		$query = $this->mdl_login->delete($this->delete('id'));
		//$this->response($message,200);
		//$query=1;
		if ($query){
			$this->response(array('status' => 1),200);
		
		}else{
			$this->response(array('status' => 0));
		}
      
    } */
	/*
	 function login_put()
    {
		
    	//$this->some_model->deletesomething( $this->get('id') );$this->_get('id')
        //$message = array('message' => 'UPDATE!', 'userid'=>$this->delete('userid'), 'name'=>$this->delete('name'));
		$data['userid']=$this->put('userid'); 
$data['username']=$this->put('username'); 
$data['password']=$this->put('password'); 
$data['level_id']=$this->put('level_id'); 

		$query = $this->mdl_login->update($data['userid'],$data);
		//$this->response($message,200);
		
		if ($query){
			$this->response(array('status' => 1),200);
		
		}else{
			$this->response(array('status' => 0));
		}
      
    } */
    /*
    function logins_get()
    {
        //$logins = $this->some_model->getSomething( $this->get('limit') );
        $logins = array(
			array('id' => 1, 'name' => 'Some Guy', 'email' => 'example1@example.com'),
			array('id' => 2, 'name' => 'Person Face', 'email' => 'example2@example.com'),
			3 => array('id' => 3, 'name' => 'Scotty', 'email' => 'example3@example.com', 'fact' => array('hobbies' => array('fartings', 'bikes'))),
		);
        
        if($logins)
        {
            $this->response($logins, 200); // 200 being the HTTP response code
        }

        else
        {
            $this->response(array('error' => 'Couldn\'t find any items!'), 404);
        }
    } */
/*

	public function send_post()
	{
		var_dump($this->request->body);
	}
*/
/*
	public function send_put()
	{
		var_dump($this->put('foo'));
	}
	*/
}